// pages/houseSearch1/houseSearch1.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    showCoverInp: true,
    hasData: true,
    rent: '租金',
    leaseWay: '方式',
    room: '户型',
    decorate: '装修',
    priceData: [
      { id: 0, name: '不限' },
      { id: 1, name: '800元以下' },
      { id: 2, name: '800-1200元' },
      { id: 3, name: '1200-2000元' },
      { id: 4, name: '2000-3000元' },
      { id: 5, name: '3000-5000元' },
      { id: 6, name: '5000-10000元' },
      { id: 7, name: '10000元以上' }
    ],
    leaseWayData: [
      { id: '',name: '不限' },
      { id: 0, name: '合租'},
      { id: 1, name: '整租'}
    ],
    houseData: [
      { id: 0, name: '不限' },
      { id: 1, name: '一室' },
      { id: 2, name: '二室' },
      { id: 3, name: '三室' },
      { id: 4, name: '四室' },
      { id: 5, name: '五室' },
      { id: 6, name: '五室以上' }
    ],
    decorateData: [      
      { id:'',name: '不限' },
      { id: 0, name: '毛坯' },
      { id: 1, name: '简装' },
      { id: 2, name: '精装' },
      { id: 3, name: '豪华装' }
    ],
    sortTypeData: [
      { value: '默认排序', isCheck: true },
      { value: '最新发布', isCheck: false },
      { value: '租金', isCheck: false, hasSort: true, isUp: false },
      { value: '面积', isCheck: false, hasSort: true, isUp: false },
    ],
    houseFlag: '',
    submitData: {
      orderByType: 1, //	排序方式 1、默认，2、最新发布3，租金, 4、面积			
      sortType: '', //	0降序，1升序			
      content: '', //	首页搜索			
      searchType: '', //	1、地区，2、地铁，3、学区			
      conditions: '', //	内容			
      startRent: '', //	租金开始			
      endRent: '', //	租金结束			
      leaseWay: '', //	租赁方式（0合租，1整租）			
      room: '', //	房间			
      houseSourceTypeId: '', //	房源id			
      startFloor: '', //	楼层开始			
      endFloor: '', //	楼层结束			
      decorate: '', //	装修（0毛坯，1简装，2精装，3豪华装）			
      communityName: '', //	小区姓名			
      pageSize: 10,
      pageNo: 1		
    },
    commName: '',
    houseList: [],
    hasData: true,
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      // ['submitData.houseType']: options.flag,
      houseFlag: options.flag,
      ['submitData.communityName']: options.communityName
    })
    wx.setNavigationBarTitle({
      title: options.communityName
    })
    this.getRentHouse()
  },
  inputFocus: function (e) {
    this.setData({
      showCoverInp: false
    })
  },
  inputBlur: function (e) {
    if (this.data.commName == '') {
      this.setData({
        showCoverInp: true
      })
    }
  },
  changeInput: function (e) {
    this.setData({
      ['submitData.pageNo']: 1,
      houseList: [],
      commName: e.detail.value,
      ['submitData.communityName']: e.detail.value
    })
    this.getRentHouse()
  },
  // 获取租房所有的数据
  getRentHouse: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar+ 'tGyHouseRental/home/rental',
      data: this.data.submitData,
      success: function (res) {
        _this.setData({
          loadSuccess: true
        })
        wx.hideLoading()
        if (res.data.success) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          var houses = res.data.data.list;
          for (var i = 0; i < houses.length; i++) {
            if (houses[i].picture) {
              houses[i].picture = houses[i].picture.split(",");
            }
            if (houses[i].housingCharacteristics) {
              houses[i].housingCharacteristics = houses[i].housingCharacteristics.split(",");
            }
            _this.data.houseList.push(houses[i])
          }
          _this.setData({
            houseList: _this.data.houseList
          })
          if (_this.data.houseList.length == res.data.data.total) {
            _this.setData({
              hasData: false
            })
          }
        }
      }
    })
  },
  // 更改排序方式
  changeSort: function (e) {
    var index = e.currentTarget.dataset.index;
    if (index > 1) {
      this.setData({
        ['submitData.sortType']: 0
      })
      if (this.data.sortTypeData[index].isCheck){
        this.data.sortTypeData[index].isUp = !this.data.sortTypeData[index].isUp;
        if (this.data.sortTypeData[index].isUp) {
          this.setData({
            ['submitData.sortType']: 1
          })
        }
        else {
          this.setData({
            ['submitData.sortType']: 0
          })
        }
      }
    }

    for (var i = 0; i < this.data.sortTypeData.length; i++) {
      this.data.sortTypeData[i].isCheck = false;
    }
    this.data.sortTypeData[index].isCheck = true;
    this.setData({
      ['submitData.pageNo']: 1,
      houseList: [],
      ['submitData.orderByType']: index + 1,
      sortTypeData: this.data.sortTypeData
    })
    this.getRentHouse()
  },

  // 显示隐藏类型下拉选项
  typeTap: function (e) {
    var tag = e.currentTarget.dataset.tag;
    this.hideMask(tag, this.data[tag]);
    this.setData({
      // currentFirId: 0,
      [tag]: !this.data[tag]
    })
  },
  // 隐藏所有的弹出框
  hideMask: function (tag, flag) {
    this.setData({
      decorateShow: false,
      typePriceShow: false,
      typeHouseShow: false,
      leaseWayShow: false
    })
    if (tag) {
      this.setData({
        [tag]: flag
      })
    }

  },
  // 禁止冒泡
  catchFunc: function () {

  },
  // 选中菜单
  selectType: function (e) {
    var item = e.currentTarget.dataset.item;
    var tag = e.currentTarget.dataset.tag;
    var show = e.currentTarget.dataset.show;
    var key = e.currentTarget.dataset.key;
    this.setData({
      ['submitData.pageNo']: 1,
      houseList: [],
      ['submitData.' + key]: item.id,
      currentSecId: item.id,
      [tag]: false,
      [show]: item.name
    })
    if (item.name == '不限') {
      var name;
      if (show == 'decorate') {
        name = '装修'
      }
      else if (show == 'rent') {
        name = '租金'
      }
      else if (show == 'room') {
        name = '户型'
      }
      else if (show == 'leaseWay') {
        name = '方式'
      }
      if (key == 'rent') {
        this.setData({
          ['submitData.startRent']: '',
          ['submitData.endRent']: ''
        })
      }
    }
    else{
      if(key == 'rent'){
        if (item.id == 1) {
          this.setData({
            ['submitData.endRent']: 800
          })
        }
        else if (item.id == 7) {
          this.setData({
            ['submitData.startRent']: 10000
          })
        }
        else {
          var start = item.name.split("-")[0];
          var end = item.name.split("-")[1].split("元")[0];
          this.setData({
            ['submitData.startRent']: start,
            ['submitData.endRent']: end
          })
        }
      }
    }
    this.getRentHouse()
  },
  // 进入详情页
  enterDetail: function (e) {
    var flag = e.currentTarget.dataset.flag;
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/houseDetail/houseDetail?flag=' + flag + '&name=' + item.title + '&id=' + item.id + '&communityName=' + item.communityName
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      ['submitData.pageNo']: 1,
      houseList: []
    })
    this.getRentHouse();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      ['submitData.pageNo']: this.data.submitData.pageNo + 1
    })
    this.getRentHouse();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})